<template>
  <van-nav-bar title="修改收货地址" left-arrow @click-left="onBack" />

  <van-address-edit
    :area-list="areaList"
    show-set-default
    show-delete
    :address-info="addressInfo"
    :area-columns-placeholder="['请选择', '请选择', '请选择']"
    @save="onSave"
    @delete="onDelete"
  />
</template>
<script>
  import { areaList } from '@vant/area-data'
  import { showNotify, showConfirmDialog } from 'vant'
  export default {
    data() {
      return {
        areaList,
        addressInfo: {},
        LoginAuth: {},
        addressId: 0,
      }
    },
    created() {
      let addressId = this.$route.query.addrid ? this.$route.query.addrid : 0

      this.addressId = addressId

      this.LoginAuth = this.$cookies.get('LoginAuth') ? this.$cookies.get('LoginAuth') : {}

      this.getAddressInfo(addressId)
    },
    methods: {
      onBack() {
        this.$router.push('/business/address/index')
      },
      // 修改收货地址
      async onSave(info) {
        let data = {
          addrid: this.addressId,
          busid: this.LoginAuth.id,
          consignee: info.name,
          mobile: info.tel,
          status: info.isDefault === true ? 1 : 0,
          address: info.addressDetail,
          code: info.areaCode,
        }

        let result = await this.$api.AddressEdit(data)

        if (result.code === 1) {
          showNotify({
            type: 'success',
            message: result.msg,
            duration: 1500,
            onClose: () => {
              this.$router.go(-1)
            },
          })
        } else {
          showNotify({
            type: 'warning',
            message: result.msg,
            duration: 1500,
          })
        }
      },

      // 拿传过来的id  回显收货地址信息
      async getAddressInfo(addrid) {
        let data = {
          addrid,
          busid: this.LoginAuth.id,
        }

        let result = await this.$api.AddressInfo(data)

        if (result.code === 0) {
          showNotify({
            type: 'warning',
            message: result.msg,
            duration: 1500,
            // 关闭后跳转
            onClose: () => {
              this.$router.push('/business/address/index')
            },
          })
        }

        this.addressInfo = {
          name: result.data.consignee,
          tel: result.data.mobile,
          areaCode: result.data.district,
          addressDetail: result.data.address,
          isDefault: result.data.status == 1 ? true : false,
        }
      },

      onDelete(info) {
        showConfirmDialog({
          title: '删除',
          message: '确认删除该收货地址吗？',
        })
          .then(async () => {
            let data = {
              addrid: this.addressId,
              busid: this.LoginAuth.id,
            }

            let result = await this.$api.AddressDelete(data)

            if (result.code === 1) {
              showNotify({
                type: 'success',
                message: result.msg,
                duration: 1500,
                onClose: () => {
                  this.$router.go(-1)
                },
              })
            } else {
              showNotify({
                type: 'warning',
                message: result.msg,
                duration: 1500,
              })
            }
          })
          .catch(() => {
            // on cancel
          })
      },
    },
  }
</script>
